//Generator 应用

//案例一：发号器
function* createIdMaker() {
  let id = 1;
  while (true) {
    yield id++;
  }
}
const c = createIdMaker();
console.log(c.next().value); //1
console.log(c.next().value); //2
console.log(c.next().value); //3

//案例二：实现iterable方法

const todos = {
  life: ["吃饭", "睡觉", "做梦"],
  learn: ["语文", "数学", "英语"],
  work: ["喝茶"],
  [Symbol.iterator]: function* () {
    const all = [...this.life, ...this.learn, ...this.work];
    for (const item of all) {
      yield item;
    }
    // let index = 0;
    // return {
    //   next: function () {
    //     return {
    //       value: all[index],
    //       done: index++ >= all.length,
    //     };
    //   },
    // };
  },
};
for (const item of todos) {
  console.log(item);
}
